

package com.hazelcast.instance.impl.executejar;

import com.hazelcast.internal.util.StringUtil;

import java.lang.reflect.Method;

import static java.lang.reflect.Modifier.isPublic;
import static java.lang.reflect.Modifier.isStatic;

class MainMethodFinder {

    Method mainMethod;

    private String errorMessage;

    public String getErrorMessage() {
        return errorMessage;
    }

    public Method getMainMethod() {
        return mainMethod;
    }

    boolean hasError() {
        return !StringUtil.isNullOrEmpty(errorMessage);
    }

    public void findMainMethod(ClassLoader classLoader, String mainClassName) throws ClassNotFoundException {
        Class<?> clazz = classLoader.loadClass(mainClassName);
        getMainMethodOfClass(clazz);
    }

    void getMainMethodOfClass(Class<?> clazz) {
        try {
            mainMethod = clazz.getDeclaredMethod("main", String[].class);

            if (!isPublicAndStatic()) {
                errorMessage = "Class " + clazz.getName() + " has a main(String[] args) method which is not public static";
            }
        } catch (NoSuchMethodException exception) {
            errorMessage = "Class " + clazz.getName() + " does not have a main(String[] args) method";
        }
    }

    boolean isPublicAndStatic() {
        int modifiers = mainMethod.getModifiers();
        return isPublic(modifiers) && isStatic(modifiers);
    }
}
